package com.zb.controller;
import com.zb.entity.Users;
import com.zb.util.PageUtil;
import com.zb.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;


@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("/search")
    @ResponseBody
    public PageUtil<Users> searchUsers(@RequestParam Map<String,Object> param ,
                                                                @RequestParam(required = true, defaultValue = "1") Integer index)throws Exception {
        int size = 2;
        PageUtil<Users> usersPage  = usersService.queryUsersPageByMap(param, index, size);
        return usersPage;
    }

    @RequestMapping("/saveOrUpdateUsers")
    @ResponseBody
    public Integer saveOrUpdateUsers(Users users){
        Integer flag=0;
        try {
            if(users.getUserId()==null || users.getUserId()==0){
                flag=usersService.qdtxAddUsers(users);
            }else{
                flag=usersService.qdtxModifyUsers(users);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("/getUsersById")
    @ResponseBody
    public Users getUsersById(Long id){

        Users users=null;
        try {
            users=usersService.getUsersById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    @RequestMapping("/batchRemoveUsers")
    @ResponseBody
    public Integer batchRemoveUsers(String ids){
        Integer flag=0;
        try {
            flag=usersService.qdtxBatchDeleteUsers(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("/removeUsersById")
    @ResponseBody
    public Integer removeUsersById(Long id){
        Integer flag=0;
        try {
            flag=usersService.qdtxDeleteUsersById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
